package cn.zhangxin.pethome.system.pet.service.impl;

import cn.zhangxin.pethome.system.department.domain.Department;
import cn.zhangxin.pethome.system.pet.domain.PetType;
import cn.zhangxin.pethome.system.pet.mapper.PetTypeMapper;
import cn.zhangxin.pethome.system.pet.service.IPetTypeService;
import cn.zhangxin.pethome.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-02-06
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> getTypeTree() {
        List<PetType> all = petTypeMapper.findAll();
        //3.找儿子
        for (PetType petType : all){
            if(petType.getPid()!=null){
                for (PetType parent : all) {
                    if (petType.getPid() == parent.getId()){
                        parent.getChildren().add(petType);
                        break;
                    }
                }
            }
        }

        //2.找顶级
        List<PetType> firstTree = all.stream()
                //filter过滤器
                .filter(department -> department.getPid() == null)
                .collect(Collectors.toList());//转为list
        return firstTree;
    }
}
